package com.atguigu.eduservice.entity.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Map;

public class subjectlistener extends AnalysisEventListener<SubjectData> {
    public EduSubjectService subjectService;

    public subjectlistener(EduSubjectService subjectService) {
        this.subjectService = subjectService;
    }
    public subjectlistener() {}

    @Override
    public void invoke(SubjectData subjectData , AnalysisContext analysisContext) {

        EduSubject exitstOneSubject = this.exitstOneSubject(subjectService, subjectData.getOneSubjectName());
       if(exitstOneSubject==null){
           exitstOneSubject=new EduSubject();
           exitstOneSubject.setParentId("0");
           exitstOneSubject.setTitle(subjectData.getOneSubjectName());
           subjectService.save(exitstOneSubject);
       }

       String pid=exitstOneSubject.getId();
        EduSubject exitstTwoSubject = this.exitstTwoSubject(subjectService, subjectData.getTwoSubjectName(), pid);
   if(exitstTwoSubject==null){
       exitstTwoSubject=new EduSubject();
       exitstTwoSubject.setParentId(pid);
       exitstTwoSubject.setTitle(subjectData.getTwoSubjectName());
       subjectService.save(exitstTwoSubject);
   }
    }
    private EduSubject exitstOneSubject(EduSubjectService subjectService,String name){
        QueryWrapper<EduSubject> wrapper=new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id","0");
        EduSubject oneSubject = subjectService.getOne(wrapper);
   return oneSubject;
    }
    private EduSubject exitstTwoSubject(EduSubjectService subjectService,String name,String pid){
        QueryWrapper<EduSubject> wrapper=new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",pid);
        EduSubject twoSubject = subjectService.getOne(wrapper);
        return twoSubject;
    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
